package com.beiding.fastmock.fram;

import com.beiding.fastmock.utils.HttpUtils;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.request.async.DeferredResult;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 丁常磊
 * @date 2021/7/14 13:07
 */


public class RequestHolder extends HttpBase {
    @Getter
    @Setter
    private Map<String, Object> variable;
    @Getter
    @Setter
    private Map<String, Object> param;
    @Getter
    @Setter
    private HttpServletRequest request;
    @Getter
    @Setter
    private HttpServletResponse response;
    @Getter
    @Setter
    HttpSession session;

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private String basePath;

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private Object jsonBody;

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private String strBody;

    //控制器对象
    ScriptObjectMirror controller;

    public Map<String, Object> getV() {
        return getVariable();
    }

    public MultiValueMap<String, String> getH() {
        return getHeader();
    }

    public Map<String, Object> getP() {
        return getParam();
    }


    public String getMethod() {
        return request.getMethod();
    }

    public String getPath() {
        return request.getServletPath();
    }

    public String getBasePath() {
        if (basePath != null) {
            return basePath;
        }
        String s = request.getRequestURL().toString();
        return basePath = s.substring(0, s.length() - getPath().length());
    }


    public void writeFile(File currentFile, String dir) {
        writeFile(currentFile, dir, Collections.emptyMap());
    }

    /**
     * 写文件
     *
     * @param dir
     * @throws IOException
     */
    public void writeFile(File currentFile, String dir, Map<String, Object> conf) {
        File file = new File(currentFile, dir);
        if (!file.exists() || !file.isFile()) {
            setStatus(404);
            return;
        }

        //支持端点续传

        HttpUtils.loadFile(file, request, response, conf);
    }

    public Map<String, Object> getRawParam() {
        //获取原始的param
        String queryString = getRequest().getQueryString();

        if (queryString == null) {
            return null;
        }
        String[] split = queryString.split("&");
        Map<String, Object> map = new HashMap<>();
        for (String s : split) {
            int i = s.indexOf("=");
            if (i > -1) {
                map.put(s.substring(0, i), s.substring(i + 1));
            }
        }
        return map;
    }

    /**
     * 重定向
     *
     * @param url
     */
    public void redirect(String url) {
        setStatus(302);
        getResponse().setHeader("Location", url);
    }

    /**
     * 重写路径
     *
     * @param from
     * @param to
     * @return
     */
    public String rewritePath(String from, String to) {
        return getPath().replaceAll(from, to);
    }

    /**
     * 设置响应头
     *
     * @param status
     */
    public void setStatus(Integer status) {
        getResponse().setStatus(status);
    }

    public DeferredResult async() {
        return new DeferredResult();
    }

    public DeferredResult async(long l) {
        return new DeferredResult(l);
    }

    public DeferredResult async(long l, Object dr) {
        return new DeferredResult(l, dr);
    }

}
